<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>增加商品类型属性</title>
    <!-- title -->
    <% include ../public/page_title.html%>
</head>
<body class="main_body">
    <fieldset class="layui-elem-field" style="margin:10px;">
        <legend>增加商品类型属性</legend>
        <div class="layui-field-box">
            <div class="layui-row">
                <div class="layui-col-md9">
                    <form class="layui-form" style='padding:15px 0;'>
                        <div class="layui-form-item">
                            <label class="layui-form-label">属性名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" class="layui-input newsName" lay-verify="title" placeholder="类型名称">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">所属类型</label>
                            <div class="layui-input-block">
                                <select name="cate_id" lay-verify="cate_id" lay-filter="cate_id">
                                    <%for(var i=0;i<list.length;i++){%>
                                        <option <%if(list[i]._id==id){%>selected<%}%> value="<%=list[i]._id%>"><%=list[i].title%></option>
                                    <%}%>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">录入方式</label>
                            <div class="layui-input-block">
                                <input type="radio" lay-filter="attr_type" name="attr_type" value="1" title="单行文本框" checked />
                                <input type="radio" lay-filter="attr_type" name="attr_type" value="2" title="多行文本框" />
                                <input type="radio" lay-filter="attr_type" name="attr_type" value="3" title="select下拉框(以英文逗号分隔)" />
                            </div>
                        </div>
                        <div class="layui-form-item" id='value'>
                            <label class="layui-form-label">可选列表</label>
                            <div class="layui-input-block">
                                <textarea id="attr_value" name="attr_value" placeholder="以英文逗号分隔" class="layui-textarea"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="addType">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                <a href="<%=adminName%>/goodsTypeAttribute?id=<%=id%>" class="layui-btn layui-btn-primary">返回</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </fieldset>
	<script type="text/javascript" src="/public/admin/layui/layui.js"></script>
    <script type="text/javascript" src="/public/admin/js/index.js"></script>
    <script>
         layui.use(['form','jquery'], function() {
            var form = layui.form,
                $ = layui.jquery;
            //自定义验证规则
            form.verify({
                title: function(value) {
                    if(value.length ==0 ) {
                        return '请输入属性名称';
                    }
                },
            });
            var attr_type=1;
            $('#value').hide()
            if(attr_type==3){
                $('#value').show()
                $('#attr_value').val('')
            }
            form.on('radio(attr_type)', function(data) {
                var val=$(this).val();
                console.log(val)
                if(val==3){
                    $('#value').show()
                   
                }else{
                    $('#value').hide()
                }
                $('#attr_value').val('')
            })
            //监听提交
            form.on('submit(addType)', function(data) {
                data.field._csrf = "<%=csrf%>";
                console.log(data.field)
                $.ajax({
                    type:'post',
                    data:data.field,
                    dataType:'json',
                    url:'<%=adminName%>/goodsTypeAttribute/doadd',
                    success:function(res){
                        console.log(res);
                        if(res.code===0){
                            layer.msg(res.msg);
                            setTimeout(function(){
                                window.location.href = "<%=adminName%>/goodsTypeAttribute?id=<%=id%>"
                            },500)
                        }else{
                            layer.msg(res.msg);
                        }
                        
                    },
                    error:function(err){
                        layer.msg('后台出错');
                    }
                })
                return false;
            })
        })
    </script>
</body>
</html>